﻿using System;
using System.Collections.Generic;
using System.Text;
using Imple.Entities;
using System.IO;
using System.Xml;
using System.Diagnostics;

namespace Imple.Parse
{
    class RSS2Parser : AbstractParser
    {
        private XmlReaderSettings _setting;
        private XmlReader _reader;
        public RSS2Parser()
        {
            InitXMLSetting();
        }

        public override Stream TargetStream
        {
            set
            {
                base.TargetStream = value;
                _reader = XmlReader.Create(_targetStream, _setting);
            }
        }

        private void InitXMLSetting()
        {
            _setting = new XmlReaderSettings();
            _setting.IgnoreComments = true;
            _setting.IgnoreWhitespace = true;
        }

        public override bool ParseNext()
        {
            Stream stream = base._targetStream;
            if (null == stream)
                throw new ArgumentNullException("stream");
            if (_reader.EOF)
                return false;

            XmlNodeType nodeType = _reader.NodeType;
            if (XmlNodeType.Attribute == nodeType || XmlNodeType.EndElement == nodeType)
                return true;

            string localname = _reader.LocalName;

            _currentNode = new ParseNode();
            _currentNode.Level = _reader.Depth;
            _currentNode.LocalName = _reader.LocalName;
            _currentNode.NodeType = _reader.NodeType;
            _currentNode.Value = _reader.Value;

            return true;
        }

        public override ParseNode CurrentNode
        {
            get { return base._currentNode; }
        }

        public override bool ValidateTree(ParseTree tree)
        {
            /*
             * base on RSS2.0 protocol
             * parse specified tree
             */
            return true;
        }
    }
}
